package com.heima.behavior.controller;


import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.IApReadService;
import com.heima.common.constants.KafkaConstants;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.pojos.ApRead;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.UserThreadLocal;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 文章阅读表 前端控制器
 * </p>
 *
 * @author fkn
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/api/v1")
public class ApReadController {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    @PostMapping("/read_behavior")
    public ResponseResult read(@RequestBody @Valid ReadBehaviorDto dto){
        //填充pojo
        ApRead apRead = ApRead.builder().articleId(dto.getArticleId())
                .userId(UserThreadLocal.get()).readTime(LocalDateTime.now())
                .readCount(dto.getCount()).build();
        //发送消息
        kafkaTemplate.send(KafkaConstants.BEHAVIOR_READ, JSON.toJSONString(apRead));
        return ResponseResult.okResult(null);
    }
}
